﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using MacomberMapSystem.Common.User_Interfaces.One_Lines;
using MacomberMapSystem.Common.Properties;

namespace MacomberMapSystem.Common.User_Interfaces.Database
{
    /// <summary>
    /// This class provides a login informational panel and displays the status of login events
    /// </summary>
    public class MM_Login_Panel: Panel
    {
        #region Variable declarations
        /// <summary>Our font for the main display</summary>
        private Font DrawFont = new Font("Arial", 12);

        /// <summary>The number of steps in our logon process</summary>
        public int CurStep = 0;

        /// <summary>The total number of steps in our login process</summary>
        public int NumSteps = 0;

        /// <summary>The current process being invoked</summary>
        public MM_Login_Process CurrentProcess;

        /// <summary>Our line brush for drawing our screen</summary>
        private LinearGradientBrush LineBrush;

        /// <summary>Our ImageAttributes for rendering the screen</summary>
        private ImageAttributes iAttr = new ImageAttributes();

        #endregion

        /// <summary>
        /// Initialize our login panel
        /// </summary>
        public MM_Login_Panel()
        {
            this.MinimumSize = new Size(375, 64);
            LineBrush = new LinearGradientBrush(new Rectangle(0,0,375, 64), Color.White, Color.LightGreen, 60);
            iAttr.SetColorKey(Color.FromArgb(240, 240, 240), Color.White);
            DoubleBuffered = true;
        }

        /// <summary>
        /// Handle the size change to fix our line brush
        /// </summary>
        /// <param name="e"></param>
        protected override void OnSizeChanged(EventArgs e)
        {
            if (this.Width > 0 && this.Height > 0)
            {
                LinearGradientBrush OldBrush = LineBrush;
                LineBrush = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height), Color.White, Color.LightGreen, 60);
                if (OldBrush != null)
                    OldBrush.Dispose();
                Refresh();
            }
        }

        /// <summary>
        /// Handle the rendering of our display
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(LineBrush, ClientRectangle);
            float Ratio = Resources.ERCOT_Logo.Width / Resources.ERCOT_Logo.Height;

            e.Graphics.DrawImage(Properties.Resources.ERCOT_Logo, new Rectangle(10, 7, 100, (int)(100 / Ratio)), 0, 0, Resources.ERCOT_Logo.Width, Resources.ERCOT_Logo.Height, GraphicsUnit.Pixel, iAttr);

            MM_OneLine_Element.CenterFormat.Alignment = MM_OneLine_Element.CenterFormat.LineAlignment = StringAlignment.Center;

            //Draw our message
            Font SubCaptionFont = SystemFonts.CaptionFont;
            String TextToWrite = Application.ProductName + (CurrentProcess == null ? "\nVersion " + Application.ProductVersion : "\n (" + CurStep.ToString() + "/" + NumSteps.ToString() + ") " + CurrentProcess.Title + (CurrentProcess.Err == null ? "" : " failed!"));
            Rectangle MainRect = new Rectangle(115, ClientRectangle.Top, ClientRectangle.Width - 119, ClientRectangle.Height - (CurrentProcess == null ? 0 : SubCaptionFont.Height) - 1);
            e.Graphics.DrawString(TextToWrite, DrawFont, Brushes.Black, MainRect, MM_OneLine_Element.CenterFormat);

            //If we have a current process, update accordingly.            
            if (CurrentProcess != null && CurrentProcess.TaskTotal > 0)
            {
                Size StringSize = Size.Ceiling(e.Graphics.MeasureString(TextToWrite, DrawFont));
                Rectangle DrawRect = new Rectangle(MainRect.Left + (MainRect.Width / 2) - (StringSize.Width / 2), MainRect.Bottom, StringSize.Width, SubCaptionFont.Height);
                e.Graphics.FillRectangle(Brushes.LightBlue, DrawRect.Left, DrawRect.Top, DrawRect.Width * CurrentProcess.TaskCurrent / CurrentProcess.TaskTotal, DrawRect.Height);
                e.Graphics.DrawRectangle(Pens.Black, DrawRect);
                e.Graphics.DrawString(((float)CurrentProcess.TaskCurrent / (float)CurrentProcess.TaskTotal).ToString("0.0%"), SystemFonts.CaptionFont, Brushes.Black, DrawRect, MM_OneLine_Element.CenterFormat);
            }
        }
    }
}
